package genetic;

public class evolve
{
	public static void evolve(String target)
	{
		int lengthOfStr = target.length();

		int popSize = 300;
		int generation = 0;

		// array of strings will be the population
		String[] population = new String[popSize];

		// first random population
		for (int i = 0; i < popSize; i++)
		{
			// random string for population element i
		//	population[i] = RandomStringUtils.randomAscii(lengthOfStr);
		}

		boolean found = false;
		// found = true;
		while (!found)
		{
			// main genetic algo. loop
			System.out.print("Generation: " + generation + " | ");

			// create children array and populate via
			// mating/crossover/combination
			String[] children = new String[popSize];
			String fittest = "";
			int thisrating = 999999999; // just some really poor rating
			int rating = 0;
			for (int i = 0; i < popSize && !found; i++)
			{
//				rating = fitness(target, population[i]);
				// check the genomes for a win
				if (rating == 0)
				{
					System.out.println(population[i] + " | 0");
					System.out.println("Found match, pop num: " + i);
					found = true;
				}
				if (rating < thisrating)
				{
					fittest = population[i];
				}

				// build the next generation
				// using tournament selection to select two parents from 4
				// random
//				String[] mated = tournament(population[rand.nextInt(popSize)],
//						population[rand.nextInt(popSize)],
//						population[rand.nextInt(popSize)],
//						population[rand.nextInt(popSize)]);

				// this randomly selects one of two children created by the
				// tournament mating
//				if (rand.nextBoolean())
				{
//					children[i] = mated[0];
				}
//				else
				{
//					children[i] = mated[1];
				}

			}

			// children array has been made, now replace the parents with the
			// children and do again
			for (int i = 0; i < popSize; i++)
			{
				population[i] = children[i];
			}

			// print out the fittest and it's rating
			if (!found)
			{
				System.out.println(fittest + " | " + rating);
			}

			// increment generation
			generation++;
		}

		// finished the genetic algo
		System.out.println("Found in " + generation + " generations.");

	}
}